/** 
 * @Title: ATest.java 
 * @Package com.zizo.test 
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月25日 下午4:07:19 
 */
package com.zizo.autohome.sta.mix;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.zizo.util.ConnectionPool;

/**
 * @ClassName: ATest
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月25日 下午4:07:19
 * 
 */
public class MixUtil {
	static final Connection conn = ConnectionPool.getConnection("autohome_other", "localhost");

	public static final void getID() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT bp_mid,bp_name,bp_brdid,bp_brname FROM autohome_other.other_core_basicparam GROUP BY bp_mid,bp_brdid";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				String bp_name = rs.getString(2);
				int bp_mid = rs.getInt(1);
				String bp_brname = rs.getString(4);
				int bp_brid = rs.getInt(3);
				update(bp_brid, bp_brname, bp_mid, bp_name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void update(int brid, String brname, int mid, String mname) {
		PreparedStatement ps = null;
		String sql = "UPdate carvoice_biz.biz_mix set mix_brdname=?,mix_cname=? where mix_brdid=? and mix_cid=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, brname);
			ps.setString(2, mname);
			ps.setInt(3, brid);
			ps.setInt(4, mid);
			ps.executeUpdate();
			System.out.println(ps.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void getIDS() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT price_cid,price_mid,YEAR(price_btime),AVG(price_lcj),COUNT(price_mid) FROM autohome_other.other_price WHERE price_istrue=1 GROUP BY price_cid,price_mid,YEAR(price_btime);";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int brdid = rs.getInt(1);
				int cid = rs.getInt(2);
				int tyear = rs.getInt(3);
				float alcj = rs.getFloat(4);
				int ocounts = rs.getInt(5);
				MixDao mix = new MixDao(conn, brdid, cid, tyear, alcj, ocounts);
				mix.toString();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		Statement st = null;
		ResultSet rs = null;
		ExecutorService fix = Executors.newFixedThreadPool(20);
		String sql = "SELECT price_cid,price_mid,YEAR(price_btime),AVG(price_lcj),COUNT(price_mid) FROM autohome_other.other_price WHERE price_istrue=1 GROUP BY price_cid,price_mid,YEAR(price_btime);";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int brdid = rs.getInt(1);
				int cid = rs.getInt(2);
				int tyear = rs.getInt(3);
				float alcj = rs.getFloat(4);
				int ocounts = rs.getInt(5);
				MixDao mix = new MixDao(conn, brdid, cid, tyear, alcj, ocounts);
				fix.execute(mix);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		fix.shutdown();
	}
}
